import { request } from '@/utils/request'
import type { LoginForm, UserInfo, RegisterForm, UpdateUserForm } from '@/types/user'

// 用户登录
export const login = (data: LoginForm) => {
  return request.post<{
    token: string
    userInfo: UserInfo
  }>('/auth/login', data)
}

// 用户注册
export const register = (data: RegisterForm) => {
  return request.post<UserInfo>('/auth/register', data)
}

// 获取用户信息
export const getUserInfo = () => {
  return request.get<UserInfo>('/user/info')
}

// 更新用户信息
export const updateUserInfo = (data: UpdateUserForm) => {
  return request.put<UserInfo>('/user/info', data)
}

// 修改密码
export const changePassword = (data: {
  oldPassword: string
  newPassword: string
}) => {
  return request.put('/user/password', data)
}

// 上传头像
export const uploadAvatar = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post<{
    url: string
  }>('/user/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 用户登出
export const logout = () => {
  return request.post('/auth/logout')
}

// 刷新token
export const refreshToken = () => {
  return request.post<{
    token: string
  }>('/auth/refresh')
}

// 发送验证码
export const sendVerificationCode = (email: string) => {
  return request.post('/auth/send-code', { email })
}

// 重置密码
export const resetPassword = (data: {
  email: string
  code: string
  newPassword: string
}) => {
  return request.post('/auth/reset-password', data)
}

// 验证邮箱
export const verifyEmail = (data: {
  email: string
  code: string
}) => {
  return request.post('/auth/verify-email', data)
}